package com.insurance.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.insurance.bean.Blog;
import com.insurance.service.BlogService;


@Transactional
public class BlogServiceBean implements BlogService {
	@Resource private SessionFactory sessionFactory;

	public void save(Blog blog){
		sessionFactory.getCurrentSession().persist(blog);
	}
	
	public void update(Blog blog){
		sessionFactory.getCurrentSession().merge(blog);
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Blog getBlog(Integer blogid){
		return (Blog)sessionFactory.getCurrentSession().get(Blog.class, blogid);
	}

	public void delete(Integer blogid){
		sessionFactory.getCurrentSession().delete(
				sessionFactory.getCurrentSession().load(Blog.class, blogid));
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Blog> getBlogs(){		
		return sessionFactory.getCurrentSession().createQuery("from Blog").list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Blog> getBlogsByWriter(String writer){		
		return sessionFactory.getCurrentSession().
		createQuery("from Blog b where b.writer="+"writer").list();
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Blog> getBlogsTop10(){		
		return sessionFactory.getCurrentSession().
		createQuery("from Blog order by id desc").setFirstResult(0).setMaxResults(10).list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Blog> getBlogsTop5(){		
		return sessionFactory.getCurrentSession().
		createQuery("from Blog order by id desc limit 0,5").setFirstResult(0).setMaxResults(5).list();
	}
}
